class StudChooseCourse {
    constructor(divId, tableId, tableFilter) {


        let load = this;
        this.divId = divId;

        this.tableId = tableId;

        this.tableFilter = tableFilter;

        this.sid = '';

        this.courseList = {};

        this.tableData = {};

        // 注意此时不能在这里获取chooseBtn,因为此时表格还没有渲染，所以获取不到选择器所对应的元素，因此是未定义的
        // this.chooseBtn = $("div[lay-id="+this.tableId+"] tr");





        this.tableSetup = {
            id: load.tableId,
            elem: "#" + load.tableId
            , defaultToolbar: ['filter', 'print', 'exports']
            // , height: 312
            , url: '/Lock/GetSomeCourses' //数据接口
            , toolbar: true
            , where: { idCard: this.idCard, accrank: 3 }
            , page: true    //开启分页
            , cols: [[ //表头
                { field: 'cid', title: '课程号', width: '10%', sort: true, /*fixed: 'left'*/ }
                , { field: 'cname', title: '课程名', edit: "text", width: '10%' }
                , { field: 'credit', title: '学分', edit: "text", width: '10%' }
                , { field: 'avgScore', title: '平均分', width: '10%', sort: true }
                , { field: 'minScore', title: '最低分', width: '10%' }
                , { field: 'maxScore', title: '最高分', width: '10%' }
                , {
                    field: 'tname', title: '任教老师',
                    templet: function (c) {
                        return c.teacher.tname;
                    }
                    , width: '10%'
                }
                , {
                    field: 'edit', title: '选项',
                    templet: "#choose-course-table-tool"
                    , width: '20%', sort: true, align: "center"
                }
            ]]
            , contentType: 'jsonp'//一定要是jsonp类型
            , parseData: function (res) { //res 即为原始返回的数据

                // console.log(res.status)
                console.log("myteacher+:")
                console.log(res);

                return {
                    "code": res.statue, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.count, //解析数据长度
                    "data": res.courseList //解析数据列表
                };
            }
            , request: {
                pageName: 'pageInd' //页码的参数名称，默认：page
                , limitName: 'rowCount' //每页数据量的参数名，默认：limit
            }
            , done: function (res, curr, count) {
                console.log("studChooseCourse");
                load.tableData = res.data;
               load.renderBtn(res.data);
            }
        }

    }
    start(sid) {
        let load = this;
        $("#" + load.divId).show().siblings().hide();

        console.log("choose-course");
        console.log(sid);

        if (sid) {
            load.sid = sid;
        }

        load.fisrtRender();

    }

    renderBtn(res){
        console.log("renderBtn---------------------");
        let load = this;
        let data = res;
        console.log(res);
        console.log(load.courseList);
        for (let i = 0; i < data.length; i++) {
            let e = data[i];
            console.log(e.cid);
            console.log(load.courseList[e.cid]);
            if (load.courseList[e.cid]) {
                let btn = $("div[lay-id="+this.tableId+"] tr").eq(i+1).find('a')[0];
                // console.log();
                // console.log($("div[lay-id=id-layui-body-stud-details-choose-Course-table] tr"));
                // console.log(btn);
                // console.log(load.chooseBtn);
                // console.log($("div[lay-id=id-layui-body-stud-details-choose-Course-table] tr"))
                $(btn).addClass("layui-btn-disabled").siblings().removeClass("layui-btn-disabled");
            }
        }
    }
    fisrtRender() {
        // 获取Course列表
        let load = this;
        $.get("/Lock/GetStudEle", { 'sid': load.sid, 'pageInd': 1, 'rowCount': 0x3f3f3f3f }, function (resp) {
            console.log("getCOurses----------------------------------------");
            console.log(load.sid)
            console.log(resp);

            let data = resp.list;
            load.courseList = {};

            for (let i = 0; i < data.length; i++) {
                load.courseList[data[i]['cid']] = true;
            }

            console.log(load.courseList);


            load.render();

        });
    }

    render() {

        let load = this;
        let table = layui.table;

        layui.table.render(load.tableSetup);

        this.addEvent();
    }

    addEvent() {

        let load = this;

        let table = layui.table;

        let form = layui.form;

        table.on('tool(' + load.tableFilter + ')', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"

            var layEvent = obj.event;
            var data = obj.data;
            var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）

            console.log("tool-choose-course");
            console.log(data);
            console.log(tr);



            var btnLoad = this;
            console.log("btnLoad");
            console.log(btnLoad);

            // 即在禁用状态点击其效果无效
            if ($(btnLoad).prop("class").indexOf(MyUtils.tableBtnDistabled) != -1) {
                console.log("disabled");
                return;
            }


            let tsid = load.sid;

            let tcid = data['cid'];





            if (layEvent === 'choosed') { //编辑
                console.log("choosed-------------------------");

                // 对后端发出更新请求
                $.ajaxSetup({ contentType: "application/json; charset=utf-8" });
                $.get("/Lock/Add/Ele", { 'sid':tsid,'cid': tcid }, function (resp) {
                    if (resp.statue == 0) {
                        // flag = true;
                        // $(btnLoad).addClass("layui-btn-disabled").siblings().removeClass("layui-btn-disabled");
                        // 将选中的课程号写入课程列表中，以避免重复请求
                        load.courseList[tcid] = true;
                        layer.msg(resp.message, { icon: 6 });
                        console.log("reneennenennd----------------");
                        layui.table.render(load.tableSetup);

                    } else {
                        layer.msg(resp.message, { icon: 5 });
                    }
                    
                })


            } else if (layEvent === 'unchoose') {

                let flag = false;

                layer.confirm('确认撤销该课程吗？', function (index) {

                    layer.close(index);
                    //向服务端发送删除指令
                    $.get("/Lock/Delete/Ele", { 'cid': tcid,'sid':tsid }, function (resp) {
                        if (resp.statue == 0) {
                            $(btnLoad).addClass("layui-btn-disabled").siblings().removeClass("layui-btn-disabled");
                            // 将未选中的课程号写入课程列表中，以避免重复请求
                            load.courseList[tcid] = false;
                            layer.msg(resp.message, { icon: 6 });

                        } else {
                            layer.msg(resp.message, { icon: 5 });
                        }
                    })

                });

            }
        });

    }


}